package com.cunjin.base.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cunjin.base.service.BaseService;


public class BaseController<T> {
	
	@Autowired
	protected BaseService<T> service;
	
	/**
	 * 1.添加一条记录
	 * @ResponseBody：让InternalResourceViewResolver不起作用
	 */
	@RequestMapping("/add")
	@ResponseBody	
	public Map<String, Object> insert(T entity){
		Map<String, Object> map = service.insert(entity);
		return map;
	}
	/**
	 * 2.根据id删除记录
	 */
	@RequestMapping("/deleteById")
	@ResponseBody	
	public Map<String, Object> deleteById(T entity){
		Map<String, Object> map = service.deleteByid(entity);
		return map;
	}
	/**
	 * 3.更新记录
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(T entity){
		Map<String, Object> map = service.update(entity);
		return map;
	}
	/**
	 * 4.根据id查询
	 */
	@RequestMapping("/queryById")
	@ResponseBody
	public Map<String, Object> queryById(T entity){
		Map<String, Object> map = service.queryById(entity);
		return map;
	}
	
	/**
	 * 5.查询所有记录
	 */
	@RequestMapping("/queryAll")
	@ResponseBody
	public Map<String, Object> queryAll(){
		Map<String, Object> map = service.queryAll();
		return map;
	}
	
	/**
	 * 6.分页查询
	 * @param page 页数，每页的记录条数
	 */
	@RequestMapping("/queryByPage/{pageIndex}/{pageSize}")
	@ResponseBody
	public Map<String, Object> queryByPage(
			@PathVariable(value="pageIndex") Integer pageIndex,
			@PathVariable(value="pageSize") Integer pageSize){
		Map<String, Object> map = service.queryByPage(pageIndex,pageSize);
		return map;
	}
}
